<?php

class IndexAction extends CommonAction
{

    public function _initialize() {
        parent::_initialize();
    }

    public function about() {
        $data = $this->getCategoryArticle('about');
        $this->assign('row', $data['row']);
        $this->assign('category', $data['category']);
        $this->display('item');
    }
    
    public function skinnova() {
        $data = $this->getCategoryArticle('skinnova');
        $this->assign('row', $data['row']);
        $this->assign('category', $data['category']);
        $this->display('item');
    }
    
    public function sendFeedback(){
        $title = $this->_post('title');
        $email = $this->_post('email');
        $feedback = $this->_post('feedback');
        try {
            if(empty($title)) {
                throw_exception('請輸入標題');
            }
            if(empty($email)) {
                throw_exception('請輸入聯系名稱');
            }
            // if( !isEmail($email) ) {
            //     throw_exception('郵件格式不正確');
            // }
            if(empty($feedback)) {
                throw_exception('請輸入留言信息');
            }
            $data['title'] = $title;
            $data['email'] = $email;
            $data['feedback'] = $feedback;
            $data['create_at'] = date('Y-m-d H:i:s', time());
            $data['create_ip'] = get_client_ip();
            $m = M('Feedback');
            if (!$m->autoCheckToken($_POST)){
                throw_exception('不能重復提交表單');
            }
            if ( $m->data($data)->add()) {
                $this->success('謝謝您的留言');
            } else {
                $this->error('留言失敗，請重試');
            }
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

    public function index() {
        C('LAYOUT_NAME', 'Layout/home');
        $art = M('Article');
        $item = $art->where('identify = "homepage"')->find();
        $this->assign('item', $item);
        $this->assign('homePage', true);
        $this->display('index');
    }
    
    public function csv(){
        $file = fopen('aa.csv','r'); 
          $fpr = fopen('do.csv', 'w');
      fputcsv($fp,array('aaa','bbb','cccc'));

       
        while ($data = fgetcsv($file)) { //每次读取CSV里面的一行内容
            echo $data[0];exit;
            fputcsv($fpr,array($data[0],$data[0]));

         }

         fclose($fpr);
         fclose($file);
    }

}